package com.thinkgem.jeesite.frame.pm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thinkgem.jeesite.common.persistence.BaseConstants;
import com.thinkgem.jeesite.frame.pm.dao.PmDataDictMapper;
import com.thinkgem.jeesite.frame.pm.entity.PmDataDict;
import com.thinkgem.jeesite.frame.pm.entity.PmDataDictItem;
import com.thinkgem.jeesite.frame.pm.utils.PmCacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 数据字典Service
 *
 * @author kongxiangsheng
 * @version 2018-08-31
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PmDataDictService extends ServiceImpl<PmDataDictMapper, PmDataDict> implements IService<PmDataDict> {
    @Autowired
    private PmDataDictItemService pmDataDictItemService;

    /**
     * 根据id获取（含子表）
     *
     * @param id
     * @return
     */
    public PmDataDict getWithChildren(String id) {
        PmDataDict pmDataDict = this.getById(id);
        pmDataDict.setPmDataDictItemList(pmDataDictItemService.getByDictCode(pmDataDict.getDictCode()));
        return pmDataDict;
    }

    /**
     * 根据字典编码获取
     *
     * @param dictCode
     * @return
     */
    public PmDataDict getByDictCode(String dictCode) {
        QueryWrapper<PmDataDict> wrapper = new QueryWrapper();
        wrapper.eq(PmDataDict.COL_DICT_CODE,dictCode);
        return this.baseMapper.selectOne(wrapper);
    }

    /**
     * 保存数据（含子表）
     *
     * @param pmDataDict
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveWithChildren(PmDataDict pmDataDict) {
        // 清除缓存
        PmCacheUtils.removeDataDictItems(pmDataDict.getDictCode());

        // 保存字典
        this.saveOrUpdate(pmDataDict);

        // 保存字典条目
        for (PmDataDictItem pmDataDictItem : pmDataDict.getPmDataDictItemList()) {
            if (pmDataDictItem.getId() == null) {
                continue;
            }
            if (BaseConstants.DEL_FLAG_NORMAL.equals(pmDataDictItem.getDelFlag())) {
                // TODO: 有没有办法不用加下面这行
                pmDataDictItem.setDataDictId(pmDataDict.getId());
                pmDataDictItemService.saveOrUpdate(pmDataDictItem);
            } else {
                pmDataDictItemService.removeById(pmDataDictItem.getId());
            }
        }
    }

    /**
     * 删除数据（含子表数据）
     *
     * @param pmDataDict
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteWithChildren(PmDataDict pmDataDict) {
        // 清除缓存
        pmDataDict = this.baseMapper.selectById(pmDataDict.getId());
        PmCacheUtils.removeDataDictItems(pmDataDict.getDictCode());

        // 删除字典
        this.removeById(pmDataDict.getId());

        // 删除字典条目
        QueryWrapper queryWrapper = new QueryWrapper<PmDataDictItem>();
        queryWrapper.eq(PmDataDictItem.COL_DATA_DICT_ID, pmDataDict.getId());
        pmDataDictItemService.remove(queryWrapper);
    }
}